<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="加油枪编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入加油枪编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="税务机关代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parent0">
               <!-- <j-search-select-tag type="list" v-model="model.parent0" v-bind:dict="taxbind" placeholder="请选择所属税务机关" />   -->
               <j-select-depart v-model="model.parent0"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="所属加油站" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parent1">
              <j-search-select-tag type="list" v-model="model.parent1" v-bind:dict="stationbind" placeholder="请选择所属加油站" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="所属加油机" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parent2">
              <j-search-select-tag type="list" v-model="model.parent2" v-bind:dict="machinebind" placeholder="请选择所属加油机" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="可加油品" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="oilCat">
              <j-dict-select-tag type="list" v-model="model.oilCat" dictCode="oil_cat" placeholder="请选择可加油品" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="采集模块编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dtuSn">
              <a-input v-model="model.dtuSn" placeholder="请输入采集模块编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="端口号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="port">
              <j-dict-select-tag type="list" v-model="model.port" dictCode="dtuPort" placeholder="请选择端口号" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="默认价格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="price">
              <a-input v-model="model.price" placeholder="请输入默认价格"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="默认脉冲" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="maichong">
              <a-input v-model="model.maichong" placeholder="请输入默认脉冲"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col v-if="username=='superuser'" :span="24">
            <a-form-model-item  label="进制" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="temp1">
              <a-input v-model="model.temp1" placeholder="进制"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  import Vue from 'vue'
  import { USER_INFO } from "@/store/mutation-types"
  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
   import JSelectDepart from '@/components/jeecgbiz/JSelectDepart2'

  export default {
    name: 'OilGunForm',
    components: {
      JSelectDepart
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/gun/oilGun/add",
          edit: "/gun/oilGun/edit",
          queryById: "/gun/oilGun/queryById"
        },
        stationbind:"station,station_name,station_name,tax_name like 'A09%25'",
        machinebind:"gas_machine,name,name",
        taxbind:"sys_depart,depart_name,org_code",
        username:""
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      },
      getStation:function(){
        return this.model.parent1;
      }
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
      var user = Vue.ls.get(USER_INFO);
      this.username=user.username;
    },
    watch:{
        getStation:function(newval,oldval){
          //window.console.log("onchange"+newval);
          this.machinebind="gas_machine,name,name,parent='"+newval+"'";

        }
    },
    methods: {
      add () {
         var user = Vue.ls.get(USER_INFO);
      this.modelDefault.parent0=user.orgCode;
      this.stationbind="station,station_name,station_name,tax_name like '"+user.orgCode+"%25'";
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>